package com.yuxin.common.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;

/**
 * @author zehui.zeng
 * @date 13-7-27 下午3:16
 */
public class CommonUtil {

    /***
     * 获取cookie 值
     * @param request
     * @param cookieKey
     * @return
     */
    public static String getCookie(HttpServletRequest request,String cookieKey){
        Cookie[] cookies = request.getCookies();
        String cookie = null;
        if(cookies != null){
            for(int i=0;i<cookies.length;i++){
                if(cookieKey.equals(cookies[i].getName())){
                    cookie = cookies[i].getValue();
                }
            }
        }
        return cookie;
    }

    /***
     * 设置Cookie的值
     * @param response
     * @param cookieKey
     * @param cookieValue
     * @param maxAge
     */
    public static void setCookie(HttpServletResponse response,String cookieKey,String cookieValue,int maxAge,String path){
        Cookie cookie = new Cookie(cookieKey,cookieValue);
        cookie.setMaxAge(maxAge);
        cookie.setPath(StringUtils.hasText(path) ? path : "/");
        response.addCookie(cookie);
    }

    /**
     * 删除Cookie
     * @param response
     * @param cookieKey
     */
    public static void delCookie(HttpServletResponse response,String cookieKey,String path){
        Cookie cookie = new Cookie(cookieKey,null);
        cookie.setMaxAge(0);//清除cookie
        cookie.setPath(StringUtils.hasText(path) ? path : "/");
        response.addCookie(cookie);
    }

    /***
     * 判断登录是否有效
     * @return
     */
    public static boolean isLoginValid(HttpServletRequest request){
        String ticket = CommonUtil.getCookie(request,"TICKET");
        Connection conn = null;
        int count = 0;
        if(StringUtils.hasText(ticket)){
	        try {
	           conn = DBUtil.getConnection();
	           count = DBUtil.count(conn,"SELECT * FROM P_USER WHERE SRC_ID = '"+ticket+"'");
	        } catch (SQLException e) {
	            e.printStackTrace();
	        }finally {
	            DBUtil.close(conn);
	        }
        }
        return count > 0;
    }

    /***
     * 获取客户登录信息
     * @param request
     * @param flowNumber
     * @return
     * @throws SQLException
     */
    public static Map getLoginUserInfo(HttpServletRequest request) throws SQLException {
        String ticket = CommonUtil.getCookie(request,"TICKET");
        Connection conn = null;
        Map user = null;
        if(StringUtils.hasText(ticket)){
            try {
                conn = DBUtil.getConnection();
                user = DBUtil.queryUniqueResult(conn,"SELECT * FROM P_CREDIT_USER WHERE ID = ?",ticket);
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                DBUtil.close(conn);
            }
        }
        return user;
    }
}
